package axter.testrecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhaobo on 2015/12/24.
 */
public class MainActivity2 extends Activity {

	@Bind(R.id.recyclerView)
	RecyclerView recyclerView;

	@Bind(R.id.btn_del)
	Button btn_del;

	@Bind(R.id.btn_grid)
	Button btn_grid;

	List<ImageItem> dataset = new ArrayList<>(3);
	RecyclerView.LayoutManager layoutManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ButterKnife.bind(this);

		// 创建一个线性布局管理器
//		layoutManager = new LinearLayoutManager(this);
		setRecyclerViewManagerType(2);
		// 设置布局管理器
		recyclerView.setLayoutManager(layoutManager);
		// 设置动画效果
		recyclerView.setItemAnimator(new DefaultItemAnimator());

		// 创建数据集
		ImageItem item1 = new ImageItem();
		item1.w = 100;
		item1.h = 200;
		ImageItem item2 = new ImageItem();
		item1.w = 100;
		item1.h = 100;
		ImageItem item3 = new ImageItem();
		item1.w = 100;
		item1.h = 100;
		dataset.add(item1);
		dataset.add(item2);
		dataset.add(item3);
		dataset.add(item1);
		dataset.add(item2);
		dataset.add(item3);
		dataset.add(item1);
		dataset.add(item2);

		// 创建Adapter，并指定数据集
		final MyAdapter2 adapter = new MyAdapter2(dataset);
		// 设置Adapter
		recyclerView.setAdapter(adapter);


		btn_del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int pos = dataset.size() - 6;
				dataset.remove(pos);
				adapter.notifyItemRemoved(pos);
			}
		});

		btn_grid.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if ((int) recyclerView.getTag() == 1)
					setRecyclerViewManagerType(2);
				else if ((int) recyclerView.getTag() == 2) {
					setRecyclerViewManagerType(3);
				}else if((int) recyclerView.getTag() == 3){
					setRecyclerViewManagerType(1);
				}
			}
		});
	}

	protected void setRecyclerViewManagerType(int type) {
		int scrollPosition = 0;
		if (recyclerView.getLayoutManager() != null) {
			if(recyclerView
					.getLayoutManager() instanceof LinearLayoutManager) {
				scrollPosition = ((LinearLayoutManager) recyclerView
						.getLayoutManager())
						.findFirstCompletelyVisibleItemPosition();
			}
		}
		switch (type) {
			//网状
			case 1:
				layoutManager = new GridLayoutManager(this, 3);
				break;
			//线性，如list
			case 2:
				layoutManager = new LinearLayoutManager(this);
				layoutManager.canScrollHorizontally();
				break;
			case 3:
				layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
				break;
			default:
				layoutManager = new LinearLayoutManager(this);
				break;
		}
		recyclerView.setTag(type);
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.scrollToPosition(scrollPosition);
	}
}
